package com.ios.palette.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.ios.palette.dao.IStatusDAO;
import com.ios.palette.dao.IUserDAO;
import com.ios.palette.domain.Comment;
import com.ios.palette.domain.Status;
import com.ios.palette.domain.User;
import com.ios.palette.respVO.RespStatus;
import com.ios.palette.service.IStatusService;
@Service
@Cacheable(value="status")
public class StatusServiceImpl extends BaseServiceImpl<Status> implements IStatusService{
	@Autowired
	private IStatusDAO statusDao;
	@Autowired
	private IUserDAO userDao;
	
	@Override
	public Status get(String tag) {
		// TODO Auto-generated method stub
		return statusDao.findByTag(tag);
	}

//	@Override
//	public List<Status> searchByTag(String tag) {
//		// TODO Auto-generated method stub
//		return statusDao.findByTagLike(tag);
//	}
//
//	@Override
//	public List<Status> searchByDes(String description) {
//		// TODO Auto-generated method stub
//		return statusDao.findByDescriptionLike(description);
//	}

	@Override
	public List<Status> pageByHotCount(int page, int num) {
		// TODO Auto-generated method stub
		
		return statusDao.pageByHotCountOrderDesc(page, num);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RespStatus> getWorldStatusList(int userId, int page, int num) {
		// TODO Auto-generated method stub
		User user=userDao.findById(User.class, userId);
		List<Status> statusList=new ArrayList<>();
		statusList=statusDao.pageByHotCountOrderDesc(page, num);
		List<RespStatus> rsList=new ArrayList<>();
		for (Status status:statusList){
			RespStatus rs=new RespStatus();
			rs.setStatusId(status.getId());
			rs.setCreateAt(status.getCreateAt());
			rs.setUser(status.getUser());
			rs.setTitle(status.getTitle());
			rs.setArtDesc(status.getArtDesc());
			rs.setLikeCount(status.getLikeCount());
			rs.setCollectionCount(status.getCollectionCount());
			rs.setHotCount(rs.getLikeCount()+rs.getCollectionCount());
			List<String> tags=new ArrayList<String>(status.getTags());
			rs.setTags(tags);
			rs.setPics((List<String>) status.getPics());
			rs.setComments((List<Comment>) status.getComments());
			rsList.add(rs);
			if (status.getLiker().contains(user))
				rs.setLike(true);
			else rs.setLike(false);
			if (user.getCollection().contains(status))
				rs.setCollection(true);
			else rs.setCollection(false);
			if (user.getConcern().contains(status.getUser()))
				rs.setFollow(true);
			else rs.setFollow(false);
		}
		return rsList;
	}

}
